package file.day9_23;

public class SyncDemo3 {
    public static void main(String[] args) {
        Foo foo = new Foo();
        Thread t1 = new Thread(){
            public void run(){
                foo.methodA();
            }
        };

        Thread t2 = new Thread(){
            public void run(){
                foo.methodB();
            }
        };

        t1.start();
        t2.start();

    }
}
class Foo{
    Thread t = Thread.currentThread();
    public synchronized void methodA(){
        System.out.println(t.getName()+"正在执行A");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getName()+"A执行完毕");
    }

    //methodA与methodB就是互斥，锁对象是Foo的实例
    public synchronized void methodB(){
        System.out.println(t.getName()+"正在执行B");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getName()+"B执行完毕");
    }
}
